package com.cby.android.olive.ext

import android.content.res.Resources
import android.util.TypedValue

/**
 * @description: Dimension
 *
 * @author ChenBaoyang
 * @date 2021/11/4 11:28
 */

/** dip转px */
val Float.dp: Float
    get() = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        this,
        Resources.getSystem().displayMetrics
    )

/** sp转px */
val Float.sp: Float
    get() = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_SP,
        this,
        Resources.getSystem().displayMetrics
    )

val Number.dp: Int
    get() = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        this.toFloat(),
        Resources.getSystem().displayMetrics
    ).toInt()

val Int.dp: Int get() = toFloat().dp.toInt()

val Long.dp: Float get() = toFloat().dp

val Double.dp: Float get() = toFloat().dp

val Int.sp: Float get() = toFloat().sp

val Long.sp: Float get() = toFloat().sp

val Double.sp: Float get() = toFloat().sp

fun Int.pxToDp(): Int = toFloat().pxToDp()

fun Long.pxToDp(): Int = toFloat().pxToDp()

fun Double.pxToDp(): Int = toFloat().pxToDp()

/** px转dp */
fun Float.pxToDp(): Int =
    (this / Resources.getSystem().displayMetrics.density + 0.5f).toInt()

fun Int.pxToSp(): Int = toFloat().pxToSp()

fun Long.pxToSp(): Int = toFloat().pxToSp()

fun Double.pxToSp(): Int = toFloat().pxToSp()

/** px转sp */
fun Float.pxToSp(): Int =
    (this / Resources.getSystem().displayMetrics.scaledDensity + 0.5f).toInt()

